import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.Map;

/**
 *  http简单工具类仅供参考
 */
public class HttpUtil {

    /**
     *  get 请求
     * @param url
     * @param param
     * @return
     * @throws Exception
     */
    public static String doGet(String url, Map param) throws Exception{
        URIBuilder ub = new URIBuilder(url);
        ub.addParameters(buildParameters(param));
        HttpGet httpGet = new HttpGet(ub.build());
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpGet);
        String content = EntityUtils.toString(closeableHttpResponse.getEntity(),"UTF-8");
        return content;
    }


    /**
     *  post 请求
     * @param url
     * @param param
     * @return
     * @throws Exception
     */
    public static String doPost(String url, Map param) throws Exception{
        URIBuilder ub = new URIBuilder(url);
        ub.addParameters(buildParameters(param));
        HttpPost httpPost = new HttpPost(ub.build());
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpPost);
        String content = EntityUtils.toString(closeableHttpResponse.getEntity(),"UTF-8");
        return content;
    }


    /**
     * post body 请求
     * @param url
     * @param param
     * @param body
     * @return
     * @throws Exception
     */
    public static String doPost(String url, Map param,String body) throws Exception{
        URIBuilder ub = new URIBuilder(url);
        ub.addParameters(buildParameters(param));
        HttpPost httpPost = new HttpPost(ub.build());
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        StringEntity entity=new StringEntity(body);
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpPost);
        String content = EntityUtils.toString(closeableHttpResponse.getEntity(),"UTF-8");
        return content;
    }



    private static ArrayList<NameValuePair> buildParameters(Map<String, String> params) {
        ArrayList<NameValuePair> pairArrayList = new ArrayList<>();
        for (Map.Entry<String, String> param : params.entrySet()) {
            pairArrayList.add(new BasicNameValuePair(param.getKey(), param.getValue()));
        }
        return pairArrayList;
    }
}
